package com.ds.controller;

import com.ds.pojo.User;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.*;
import java.io.Writer;
import java.util.Map;
import java.util.Optional;

/**
 * @program: SpringMVC
 * @description:
 * @author: Dawson.Zhang
 * @create: 2020-04-06
 **/
@Controller
public class SpringMVC {
    public static final String SUCCESS = "success";

    @RequestMapping("/hello")
    //把请求中的参数，绑定要传参的id上
    public String index() {
        System.out.println();
        return "index";
    }

    @RequestMapping("/pathvariableTest/{id}")
    //把请求中的参数，绑定要传参的id上
    public String pathvariableTest(@PathVariable("id") Integer id) {
        System.out.println(id);
        return SUCCESS;
    }

    @RequestMapping("/requestparam")
    //把请求中的参数，绑定要传参的id上
    public String requestParam(@RequestParam("userName") String name) {
        System.out.println(name);
        return SUCCESS;
    }

    @RequestMapping("/cookievalue")
    public String cookievalue(@CookieValue("JSESSIONID") String cookie) {
        System.out.println(cookie);
        return SUCCESS;
    }



    @RequestMapping("/servletApi")
    public void servletApi(HttpServletRequest request, HttpSession session, Writer writer) throws Exception {
        System.out.println(request.getMethod());
        session.setAttribute("user", "zhangbaoguo");
        writer.write(session.getAttribute("user").toString());
    }

    @RequestMapping("/testmodelandview")
    public ModelAndView testmodelandview() {
        ModelAndView modelAndView = new ModelAndView("modelAndView");
        modelAndView.addObject("msg", "我的妈妈头发蒙啊");
        return modelAndView;
    }

    @RequestMapping("/testmodel")
    public String testModel(Model model) {
        model.addAttribute("msg", "我的妈妈头发蒙啊");
        return "modelAndView";
    }

    @RequestMapping("/addUser")
    public String addUser(@ModelAttribute("user1") User user,HttpSession session) {
        session.setAttribute("user",user);
        Object user1 = session.getAttribute("user" );
        System.out.println(user);
        return "user";
    }

    @ModelAttribute()
    public User getUser(@RequestParam(value = "id", required = false) Integer id) {
        System.out.println("@ModelAttribute标记的注解会在每个请求方法之前被调用");
        if (Optional.ofNullable(id).isPresent()) {
            User user = new User(2, "zhangbaoguo", "张保国");
            System.out.println("从数据库获取对象" + user);
        }
        return new User(2, "zhangbaoguo", "张保国");
    }

    /**
     *  运行流程：
     *      1：执行被@ModelAttribute标记的方法，返回该对象，同时会放到map集合中。键为改类名首字母小写.
     *      2：SpringMVC从map中取出user赋值给User对象对应的属性
     *      3：SpringMVC把上述对象的值赋值给参数中对象类型里面部分为null的数据(表单里面没有该字段)
     *      注意！map中的键必须要和入参类型的首字母小写的字符串一致
     * @param user
     * @return
     */
    @RequestMapping(value = "/updateuser", method = RequestMethod.POST)
    public String updateUser(User user) {
        System.out.println(user);
        return SUCCESS;
    }
    @ResponseBody
    @RequestMapping(value = "/responsebody", method = RequestMethod.GET)
    public String responseBody(@RequestBody  User user,@RequestParam("str") String str) {
        System.out.println(user);
        System.out.println(str);
        return user.toString();
    }

}
